﻿using System;
using System.Data;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyTuyenSinh.BussinessLayer;
using QuanLyTuyenSinh.Properties;

namespace QuanLyTuyenSinh.PresentationLayer
{
    public partial class frmHighSchool : Office2007Form
    {
        private int ma;

        public frmHighSchool()
        {
            InitializeComponent();
        }

        private void HideControl(bool hien)
        {
            cmdThem.Visible = hien;
            cmdLuu.Visible = hien;
            cmdXoa.Visible = hien;
            cmdThem.Enabled = true;
            cmdXoa.Enabled = true;
        }

        private void FrmTruongThptLoad(object sender, EventArgs e)
        {
            DataTable listProvince = Operator.Operator.GetListProvince();
            if (listProvince.Rows.Count > 0)
            {
                cbTinh.DataSource = listProvince;
                cbTinh.DisplayMember = "ProvinceName";
                cbTinh.ValueMember = "Id";
            }
            else
            {
                MessageBox.Show("Chưa cập nhật danh sách tỉnh");
                var frm = new frmProvince {Name = typeof (frmProvince).Name};
                frm.Visible = false;
                Hide();

                frm.ShowDialog();
            }
            DataTable listZone = Operator.Operator.GetListZone();
            if (listZone.Rows.Count > 0)
            {
                cbKV.DataSource = listZone;
                cbKV.DisplayMember = "ZoneDisplay";
                cbKV.ValueMember = "Id";
            }
            else
            {
                MessageBox.Show("Chưa cập nhật danh sách khu vực");
                var frm = new frmZone {Name = typeof (frmZone).Name};
                frm.Visible = false;
                Hide();

                frm.ShowDialog();
            }
            DataTable listDistrict = Operator.Operator.GetListHighSchool();
            if (listDistrict.Rows.Count > 0)
            {
                GrTruongTHPT.DataSource = listDistrict;
            }
            HideControl(false);
            cmdThem.Visible = true;
        }

        private void CmdThemClick(object sender, EventArgs e)
        {
            cbTinh.Focus();
            txtTentruong.Clear();
            txtDiachi.Clear();
            HideControl(true);
            ma = 0;
            cmdXoa.Visible = false;
        }

        private void CmdLuuClick(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtMatruong.Text.Trim()))
                {
                    MessageBox.Show("Chưa nhập: Mã trường");
                    return;
                }
                var ethnic = new HighSchool();
                ethnic.HighSchoolDisplay = txtMatruong.Text.Trim();
                ethnic.HighSchoolName = txtTentruong.Text.Trim();
                ethnic.HighSchoolAddress = txtDiachi.Text.Trim();
                ethnic.ProvinceId = (int) cbTinh.SelectedValue;
                ethnic.ZoneId = (int) cbKV.SelectedValue;
                DataTable listEthnic = Operator.Operator.GetListHighSchoolById(ma);
                if (listEthnic.Rows.Count > 0)
                {
                    ethnic.Id = ma;
                    int m = ethnic.Update(ethnic);
                    if (m > 0)
                    {
                        FrmTruongThptLoad(sender, e);
                    }
                }
                else
                {
                    int m = ethnic.Insert(ethnic);
                    if (m > 0)
                    {
                        FrmTruongThptLoad(sender, e);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }


        private void GrTruongThptCellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;
            if (i < 0) return;
            else
            {
                ma = int.Parse(GrTruongTHPT[0, i].Value.ToString());
                cbTinh.SelectedValue = GrTruongTHPT[1, i].Value.ToString();
                cbKV.SelectedValue = GrTruongTHPT[2, i].Value.ToString();
                txtMatruong.Text = GrTruongTHPT[3, i].Value.ToString();
                txtTentruong.Text = GrTruongTHPT[4, i].Value.ToString();
                txtDiachi.Text = GrTruongTHPT[5, i].Value.ToString();
                HideControl(true);
            }
        }

        private void CmdXoaClick(object sender, EventArgs e)
        {
            try
            {
                DialogResult DRL;
                DRL = MessageBox.Show(Resources.Delete, Resources.Header, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (DRL == DialogResult.No)
                {
                    return;
                }
                else
                {
                    var ethnic = new HighSchool();
                    ethnic.Id = ma;
                    int m = ethnic.Delete(ethnic);
                    if (m > 0)
                    {
                        FrmTruongThptLoad(sender, e);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private void FrmTruongThptKeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Control) && (e.KeyCode == Keys.S) && cmdLuu.Visible)
            {
                CmdLuuClick(sender, e);
            }
            else if (e.Control && e.KeyCode == Keys.N)
            {
                CmdThemClick(sender, e);
            }
            else if ((e.KeyCode == Keys.Delete) && (cmdXoa.Visible))
            {
                CmdXoaClick(sender, e);
            }
        }
    }
}